package com.baseDemo.web.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baseDemo.web.entity.UserEntity;
import com.baseDemo.web.server.SystemServer;
import com.baseDemo.web.service.user.UserService;
import com.share.hibernate.baseDaoSupport.PageUtil;
import com.share.logging.FixedLoggers;

@Controller
@RequestMapping(produces = "application/json;charset=UTF-8")
public class UserController implements FixedLoggers{

	@Resource
	UserService userService;

	@RequestMapping(value = "/user/changePasswod")
	public @ResponseBody
	String changePasswod(@RequestParam("password") String password,
			HttpServletRequest request) {
		return userService.changePassword(request, password);
	}

	@RequestMapping(value = "/user/getCurrentUser")
	public @ResponseBody
	UserEntity getCurrentUser(HttpServletRequest request) {
		HttpSession session = request.getSession();
		return (UserEntity) session.getAttribute(SystemServer.getInstance()
				.get("USERSESSION").toString());
	}

	/**
	 * 获取用户记录通过分页
	 * 
	 * @param page
	 * @param limit
	 * @param start
	 * @return
	 */
	@RequestMapping(value = "/user/getAllByPage")
	public @ResponseBody
	PageUtil getAllByPage(@RequestParam("page") Integer page,
			@RequestParam("limit") Integer limit,
			@RequestParam("start") Integer start) {
		return userService.getListForPageToJson(start, limit);
	}

	@RequestMapping(value = "/user/chackUserLoginId")
	public @ResponseBody
	UserEntity chackUserLoginId(@RequestParam("loginId") String loginId) {
		return userService.getByLoginId(loginId);
	}

	@RequestMapping(value = "/user/resetPassword")
	public @ResponseBody
	String resetPassword(@RequestBody UserEntity userEntity) {
		return userService.resetPassword(userEntity);
	}

	@RequestMapping(value = "/user/addUser")
	@ResponseBody
	public String addUser(@RequestBody UserEntity userEntity) {
		return "{'id':'" + userService.addUser(userEntity) + "'}";
	}

	@RequestMapping(value = "/user/destroyUser")
	@ResponseBody
	public String destroyUser(@RequestBody UserEntity userEntity) {
		return userService.delete(userEntity);
	}

	@RequestMapping(value = "/user/updateUser")
	@ResponseBody
	public String updateUser(@RequestBody UserEntity userEntity) {
		return userService.update(userEntity);
	}

	@RequestMapping(value = "/login")
	public String login() {
		return "login";
	}

	@RequestMapping(value = "/index")
	public String index() {
		return "index";
	}

	
	//@RequestMapping(value = "/doLogin/{name}/{password}",method=RequestMethod.POST, produces = "application/json;charset=UTF-8")
	//doLogin(@PathVariable("name") string name)){}
	@RequestMapping(value = "/doLogin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String doLogin(@RequestParam("userName") String userName,
			@RequestParam("password") String password,
			HttpServletRequest request, HttpServletResponse response) {
		USERLOGGER.info(request.getMethod());
		HttpSession session = request.getSession();
		return userService.doLogin(userName, password, session, request,
				response);
	}

	@RequestMapping(value = "/loginOut")
	@ResponseBody
	public String loginOut(HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession();
		return userService.loginOut(session);
	}
}
